package com.msmall.community.admin.action;

import java.io.File;

import com.msmall.base.action.HTMLBaseAction;
import com.msmall.base.util.ConstantUrl;
import com.msmall.base.util.StringUtil;
import com.msmall.community.wish.model.WishType;
import com.msmall.community.wish.service.WishManager;

public class WishAction extends HTMLBaseAction{
	  
	private static final long serialVersionUID = -1514663055247517410L;
	private Long wishid;
	private Long typeid;
	private File bg;//背景图片
	private String bgContentType;
	private String bgFileName;
	private File common;//未激活的心愿图片
	private String commonContentType;
	private String commonFileName;
	private File active;//激活的心愿图片
	private String activeContentType;
	private String activeFileName;
	
	private WishType wishType;
	private WishManager wishManager;

	public String execute(){
		page.setPageSize(20);
		wishManager.getWishTypes(page);
		page.setUrl(ConstantUrl.ADMIN_WISH_TYPE_INDEX);
		return SUCCESS;
	}
	
	public String create(){
		typeid = null;
		wishType = null;
		return SUCCESS;
	}
	
	public String update(){
		wishType = (WishType) wishManager.get(WishType.class, typeid);
		return SUCCESS;
	}
	
	public String save(){
		if(!validateImgSize(wishType.getImgSize())){
			message = getText("admin.wish.imgsize.error");
			return ERROR;
		}
		int posAmount = validatePosition(wishType.getPosition());
		if(posAmount <= 0){
			message = getText("admin.wish.position.amount.few.error");
			return ERROR;
		}
		if(posAmount < wishType.getThreshold()){
			message = getText("admin.wish.position.lessthan.threshold.error");
			return ERROR;
		}
		wishType.setPositionAmount(posAmount);
		wishManager.saveWishType(wishType, getUserAgentType(), bg, bgFileName, common, commonFileName, active, activeFileName);
		return SUCCESS;
	}

	private boolean validateImgSize(Integer size){
		if(size == null || size <=0){
			return false;
		}
		return true;
	}
	/**
	 * 验证小图片坐标，严格格式，必填;如果某个坐标有错 则删除，返回有效的坐标个数<br/>
	 * 格式：x1,y1;x2,y2;x3,y3(x,y最多为4位数字)
	 */
	private int validatePosition(String position){
		try{
			if(!StringUtil.nullOrBlank(position)){
				StringBuffer b = new StringBuffer();
				int count = 0;
				String[] positions = position.split(";");
				for(int i=0; i<positions.length; i++){
					String[] xy = positions[i].split(",");
					String x =  xy[0];
					String y = xy[1];
					try{
						int xPos = Integer.valueOf(x).intValue();
						int yPos = Integer.valueOf(y).intValue();
						if(xPos < 1000 && yPos < 1000 && xPos > 0 && yPos > 0 ){
							count++;
							b.append(positions[i]).append(";");
						}
					}catch(Exception e){}
				}
				if(b.length()>1){
					position = b.toString().substring(0, b.length()-1);
					return count;
				}
			}
		}catch(Exception e){
			return 0;
		}
		return 0;
	}
	public Long getWishid() {
		return wishid;
	}

	public void setWishid(Long wishid) {
		this.wishid = wishid;
	}

	public Long getTypeid() {
		return typeid;
	}

	public void setTypeid(Long typeid) {
		this.typeid = typeid;
	}

	public File getBg() {
		return bg;
	}

	public void setBg(File bg) {
		this.bg = bg;
	}

	public String getBgContentType() {
		return bgContentType;
	}

	public void setBgContentType(String bgContentType) {
		this.bgContentType = bgContentType;
	}

	public String getBgFileName() {
		return bgFileName;
	}

	public void setBgFileName(String bgFileName) {
		this.bgFileName = bgFileName;
	}

	public File getCommon() {
		return common;
	}

	public void setCommon(File common) {
		this.common = common;
	}

	public String getCommonContentType() {
		return commonContentType;
	}

	public void setCommonContentType(String commonContentType) {
		this.commonContentType = commonContentType;
	}

	public String getCommonFileName() {
		return commonFileName;
	}

	public void setCommonFileName(String commonFileName) {
		this.commonFileName = commonFileName;
	}

	public File getActive() {
		return active;
	}

	public void setActive(File active) {
		this.active = active;
	}

	public String getActiveContentType() {
		return activeContentType;
	}

	public void setActiveContentType(String activeContentType) {
		this.activeContentType = activeContentType;
	}

	public String getActiveFileName() {
		return activeFileName;
	}

	public void setActiveFileName(String activeFileName) {
		this.activeFileName = activeFileName;
	}

	public WishType getWishType() {
		return wishType;
	}

	public void setWishType(WishType wishType) {
		this.wishType = wishType;
	}

	public void setWishManager(WishManager wishManager) {
		this.wishManager = wishManager;
	}
}
